<template>
  <div>
    <el-dialog
      title="邮箱配置"
      :visible.sync="dialogVisible"
      width="40%"
      @close="handleColsed">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="Smtp" prop="smtp">
            <el-input v-model="ruleForm.smtp" placeholder="请输入Smtp,例如:smtp.qq.com"></el-input>
          </el-form-item>
          <el-form-item label="端口" prop="port">
            <el-input v-model="ruleForm.port"></el-input>
          </el-form-item>
          <el-form-item label="邮箱地址" prop="email">
            <el-input v-model="ruleForm.email" placeholder="请输入发件人邮箱,例如:123456789@qq.com"></el-input>
          </el-form-item>
          <!-- <el-form-item label="邮箱名" prop="eMailName">
            <el-input v-model="ruleForm.eMailName"></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item> -->
          <el-form-item label="授权码" prop="password">
            <el-input v-model="ruleForm.password" show-password placeholder="第一次授权时,请先申请授权码!!!"></el-input>
          </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="danger" plain @click="resetForm">重 置</el-button>
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  eMailSetting,
  getEmailSetting
} from '@/api/email/setting'
export default {
  name: 'seeting',
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        smtp: '',
        port: '',
        email: '',
        password: ''
      },
      rules: {
        smtp: [
          { required: true, message: '请输入Smtp', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入授权码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(flag) {
      // 获取配置信息
      getEmailSetting().then((res) => {
        this.ruleForm = res.data
      })
      this.dialogVisible = flag
    },
    // 提交已填写的配置信息
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return
        eMailSetting(this.ruleForm).then((res) => {
          this.dialogVisible = false
        })
      })
    },
    handleColsed() {
      this.$refs.ruleForm.resetFields()
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>

<style scoped>
</style>
